﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using KpBusiness.Model;
using KpBusiness.Layer;
using NHibernate;
using NHibernate.Criterion;

namespace KpBusiness.Service
{
    public class CompanyService
    {
        private static readonly ILog log = LogManager.GetLogger("Services");

        /// <summary>
        /// Get Company
        /// </summary>
        /// <param name="companyId">Provide Company Number</param>
        /// <returns>Returns Company</returns>
        public Company GetCompany(int companyId)
        {
            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenReaderSession();
            Company company = null;
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();
                company = session.Get<Company>(companyId);
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            return company;
        }

        /// <summary>
        /// Creates New Company in DB
        /// </summary>
        /// <param name="company">Provide Company</param>
        public void SaveCompany(Company company)
        {
            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenWriterSession();

            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();
                session.SaveOrUpdate(company);
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");

        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IList<Company> GetAllCompanys()
        {

            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenReaderSession();
            IList<Company> students = null;
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();

                ICriteria criteria = session.CreateCriteria(typeof(Company));
                students = criteria.List<Company>();
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            return students;
        }

        /// <summary>
        /// Get All Active Companys
        /// </summary>
        /// <returns></returns>
        public IList<Company> GetAllActiveCompanys()
        {

            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenReaderSession();
            IList<Company> students = null;
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();

                ICriteria criteria = session.CreateCriteria(typeof(Company))
                    .Add(Restrictions.Eq("Company.IsActive", true));

                students = criteria.List<Company>();
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            return students;
        }
    }
}
